package com.jbm.jbmsupplier.zp.liaotian;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.AllActivityApp;
import com.jbm.jbmsupplier.net.NetInterChat;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.zp.adapters.MyAddcononet;
import com.jbm.onehome.iface.LoginEntity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/5/23.
 */

public class MyAddContact extends AllActivityApp {
    @InjectView(R.id.image_idadd)
    ImageView imageIdadd;
    @InjectView(R.id.edit_idadd)
    EditText editIdadd;
    @InjectView(R.id.text_idadd)
    TextView textIdadd;
    @InjectView(R.id.list_idadd)
    ListView listIdadd;
    private final int ADDCON = 0001;
    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.tes_idadd)
    TextView tesIdadd;
    @InjectView(R.id.layout_idadd)
    LinearLayout layoutIdadd;
    private DialogLoading dialogLoading;
    ArrayList<LoginEntity> arrlogin = new ArrayList<>();
    LoginEntity login;

    @Override
    public int setContentView() {
        return R.layout.addconontact_lz;
    }

    @Override
    public void initIalizeView() {
        ButterKnife.inject(this);
        tetitle.setText("添加好友");
        initdate();
    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.addconontact_lz);
//        ButterKnife.inject(this);
//        tetitle.setText("添加好友");
//        initdate();
//    }

    private void initdate() {
        editIdadd.addTextChangedListener(watcher);
    }

    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ADDCON:
                    LoginEntity addcon = (LoginEntity) msg.obj;
                    if (addcon.m_code==200) {
                        setDialogding();
                        arrlogin.clear();
                        arrlogin.add(addcon);
                        if (arrlogin.size() <= 0) {
                            textIdadd.setVisibility(View.VISIBLE);
                        } else {
                            textIdadd.setVisibility(View.GONE);
                            layoutIdadd.setVisibility(View.GONE);
                            MyAddcononet adapter = new MyAddcononet(MyAddContact.this, arrlogin,handler);
                            listIdadd.setAdapter(adapter);
                        }
                    } else if (addcon.m_info.equals("request timeout")) {
                        setDialogding();
                        hint("请求超时连接失败");
                    } else if (addcon.m_info.equals("Not Found")) {
                        setDialogding();
                        textIdadd.setVisibility(View.VISIBLE);
                        layoutIdadd.setVisibility(View.GONE);
                    } else if (addcon.m_code==500){
                        textIdadd.setVisibility(View.VISIBLE);
                        layoutIdadd.setVisibility(View.GONE);
                        setDialogding();
                    }
                    break;
                case 0:
                    finish();
                    break;
            }


        }
    };

    @OnClick({R.id.teleft, R.id.layout_idadd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                finish();
                break;
            case R.id.layout_idadd:
                if (editIdadd.getText().toString().equals("")) {
                    hint("手机号不能为空");
                } else if (editIdadd.getText().toString().length() < 11) {
                    hint("请确认手机号");
                } else {
//                    SharedPreferActivity sharedPreferActivity = new SharedPreferActivity(MyAddContact.this, true);
//                    String allnamepwd= sharedPreferActivity.getNmaes();
//                     String name=  allnamepwd.substring(0,allnamepwd.indexOf(","));
//                      if (!name.equals("")){
//if (name.equals(editIdadd.getText().toString()) ){
//    hint("不能添加自己为好友");
//}else {
    dialogLoading = new DialogLoading(MyAddContact.this, "查找中.....", false);
    dialogLoading.show();
    NetInterChat netinterchat = new NetInterChat(handler);
    netinterchat.setAddconontact(ADDCON,0, editIdadd.getText().toString());
//}
//        }
                }
                break;
        }
    }


    public void setDialogding() {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
        }
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (editIdadd.getText().toString().length() > 0) {
                arrlogin.clear();
                tesIdadd.setText(editIdadd.getText().toString());
                layoutIdadd.setVisibility(View.VISIBLE);
                textIdadd.setVisibility(View.GONE);
            } else {
                layoutIdadd.setVisibility(View.GONE);

            }
        }
    };


}
